//----------------------------------------------------------------------------
// msgcmd.cpp
//
// display simple message box
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <windows.h>
#include <string>
#include <iostream>
#include "command.h"
#include "cmdline.h"
#include "util.h"

using namespace std;

namespace Swine {

//----------------------------------------------------------------------------
// Command & option names
//----------------------------------------------------------------------------

const char * const MSG_CMD 		= "msg";
const char * const CAPTION_OPT 	= "caption";
const char * const TEXT_OPT 	= "text";
const char * const ICON_OPT		= "icon";


//----------------------------------------------------------------------------
// Help
//----------------------------------------------------------------------------

const char * const MSG_HELP =
	"display messages using message box\n"
	"options:\n"
	" --caption     specify message box caption (default is 'Swine') \n"
	" --text        text to display (no default)\n"
    "               if --text is not specified, text is read from standard input.\n"
	" --icon        icon to use - one of: info, query (default), warn or stop\n";

//----------------------------------------------------------------------------
// Command class and registration
//----------------------------------------------------------------------------

class MsgCmd : public Command {

	public:

		MsgCmd() : Command( MSG_CMD, MSG_HELP ) {}

		int Execute( CmdLine & cl ) const;
};

static AddCommand <MsgCmd> cmd1_;

//----------------------------------------------------------------------------
// Execute command. If the text option is not specified, read lines from
// the command line and use those.
//----------------------------------------------------------------------------

int MsgCmd :: Execute( CmdLine & cl ) const {
	string caption = cl.ExtractOption( CAPTION_OPT, "Swine" );
	string text = cl.ExtractOption( TEXT_OPT, "" );
	string icon = cl.ExtractOption( ICON_OPT, "info" );
	cl.MustBeEmpty();

	if ( text == "" ) {
		text = ReadLines( std::cin );
	}

	MessageBox( 0, text.c_str(), caption.c_str(),  IconFromName( icon ) );
	Reply( "ok" );

	return 0;
}

}
